package com.cyd.product_service.cluster.consul;

import com.cyd.product_service.cluster.ClusterService;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.NewService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
//import com.google.common.collect.Lists;


/**
 * ConsulServiceRegister
 * Created by fangzhihong on 2017/11/25.
 */
public class ConsulServiceRegister extends AbstractConsulService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ConsulServiceRegister.class);

    private static final String CHECK_INTERVAL = "5s";

    private static final String CHECK_TIMEOUT = "2s";

    public ConsulServiceRegister() {
        super(new ConsulClient());
    }

    public ConsulServiceRegister(ConsulClient consulClient) {
        super(consulClient);
    }

    public void register(ClusterService service) {
        if (service == null) {
            throw new IllegalArgumentException("Service cannot be null");
        }
        if (!StringUtils.hasText(service.getId())) {
            throw new IllegalArgumentException("Service must have an ID to register.");
        }
        if (!StringUtils.hasText(service.getName())) {
            throw new IllegalArgumentException("Service must be a NAME to register.");
        }
        if (!StringUtils.hasText(service.getAddress())) {
            throw new IllegalArgumentException("Service must have an ADDRESS to register.");
        }
        if (service.getServiceType() == null) {
            throw new IllegalArgumentException("Must specify a service type, TCP or HTTP.");
        }
        if (ClusterService.ServiceType.TCP == service.getServiceType() && service.getPort() <= 0) {
            throw new IllegalArgumentException("A TCP type service must have a port to register.");
        }

        NewService newService = new NewService();
        newService.setId(service.getId());
        newService.setName(service.getName());
        newService.setAddress(service.getAddress());
        newService.setPort(service.getPort());

        //cyd
        List list=new ArrayList();
        list.add(service.getServiceType().name());
        list.add("weight:" + service.getWeight());

        newService.setTags(list);

        NewService.Check check = new NewService.Check();
        check.setInterval(CHECK_INTERVAL);
        check.setTimeout(CHECK_TIMEOUT);
        if (ClusterService.ServiceType.TCP == service.getServiceType()) {
            check.setTcp(service.getAddress() + ":" + service.getPort());
        } else {
            check.setHttp(service.getAddress());
        }
        newService.setCheck(check);
        consulClient.agentServiceRegister(newService);
        LOGGER.info("Register service as id=" + service.getId() + ", name=" + service.getName());

        String masterServiceKey = service.getName() + MASTER_KEY_SUFFIX;
        if (consulClient.getKVValue(masterServiceKey).getValue() == null) {
            LOGGER.info("No master registered for " + service.getName());
            consulClient.setKVValue(masterServiceKey, service.getId());
            LOGGER.info("Register as master service: " + service.getId());
        }
    }

    public void deregister(ClusterService service) {
        if (service == null) {
            throw new IllegalArgumentException("Service cannot be null");
        }
        if (!StringUtils.hasText(service.getId())) {
            throw new IllegalArgumentException("Service must have an ID to deregister.");
        }
        if (!StringUtils.hasText(service.getName())) {
            throw new IllegalArgumentException("Service must be a NAME to deregister.");
        }
        consulClient.agentServiceDeregister(service.getId());
        LOGGER.info("Deregister service: " + service.getId());
    }
}
